/**
 * 
 */
package com.zxc.transliterate;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.zxc.transliterate.baidu.TransApi;

import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.SqlConnRunner;
import cn.hutool.db.ds.simple.SimpleDataSource;
import cn.hutool.db.sql.Condition;
import cn.hutool.json.JSONUtil;

/**
 * @author Administrator
 *
 */
public class TransliterateDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		updatePlatformAirports();
//		updatePlatformAirports1();
		updatePlatformAirlines();
	}

	public static void updatePlatformAirlines() {
		String url = "jdbc:mysql://192.168.100.10:3308/trip_config?useUnicode=true&characterEncoding=UTF-8";
		String user = "root";
		String pass = "tarotrip@2024";
		SimpleDataSource ds = new SimpleDataSource(url, user, pass);
		SqlConnRunner runner = DbUtil.newSqlConnRunner(ds);
		
		List<Object> errorIds = new ArrayList<Object>();
		try {
		
			Connection conn = ds.getConnection();
			Entity query = new Entity("platform_airline");
			query.set("id1", new Condition("id", ">=", 1));
			
			List<Entity> entities = runner.find(ds.getConnection(), query);
			for (Entity entity : entities) {
				
				if(containsChinese(entity.getStr("airline_fullname_cn"))) {
					entity.set("airline_name_en", TransApi.transliterate(entity.getStr("airline_fullname_cn"),"en"));
					entity.set("airline_name_hk", TransApi.transliterate(entity.getStr("airline_fullname_cn"),"cht"));
					entity.set("airline_name_jp", TransApi.transliterate(entity.getStr("airline_fullname_cn"),"jp"));
					entity.set("airline_name_ko", TransApi.transliterate(entity.getStr("airline_fullname_cn"),"kor"));
				}else {
					entity.set("airline_fullname_cn", TransApi.transliterate(entity.getStr("airline_fullname_cn"),"en","zh"));
					entity.set("airline_name_hk", TransApi.transliterate(entity.getStr("airline_fullname_cn"),"cht"));
					entity.set("airline_name_jp", TransApi.transliterate(entity.getStr("airline_fullname_cn"),"jp"));
					entity.set("airline_name_ko", TransApi.transliterate(entity.getStr("airline_fullname_cn"),"kor"));
					
				}
				
				System.out.println(JSONUtil.toJsonStr(entity));
				
				Entity where = new Entity();
				where.set("id", entity.get("id"));
				try {
					runner.update(conn, entity, where);
				} catch (Exception e) {
					e.printStackTrace();
					conn = ds.getConnection();
					errorIds.add(entity.get("id"));
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(StringUtils.join(errorIds, ","));
	}
	
	
	public static void updatePlatformAirports() {
		String url = "jdbc:mysql://192.168.100.10:3308/trip_config?useUnicode=true&characterEncoding=UTF-8";
		String user = "root";
		String pass = "tarotrip@2024";
		SimpleDataSource ds = new SimpleDataSource(url, user, pass);
		SqlConnRunner runner = DbUtil.newSqlConnRunner(ds);
		
		List<Object> errorIds = new ArrayList<Object>();
		try {
		
			Connection conn = ds.getConnection();
			Entity query = new Entity("platform_airports");
			query.set("id1", new Condition("id", ">=", 1));
			
			List<Entity> entities = runner.find(ds.getConnection(), query);
			for (Entity entity : entities) {
				System.out.println(JSONUtil.toJsonStr(entity));
				
				if(StringUtils.isNotBlank(entity.getStr("name"))) {
					entity.set("ename", TransApi.transliterate(entity.getStr("name"),"en"));
					entity.set("hkname", TransApi.transliterate(entity.getStr("name"),"cht"));
					entity.set("namejp", TransApi.transliterate(entity.getStr("name"),"jp"));
					entity.set("nameko", TransApi.transliterate(entity.getStr("name"),"kor"));
				}
				
				if(StringUtils.isNotBlank(entity.getStr("city"))) {
					entity.set("ecity", TransApi.transliterate(entity.getStr("city"),"en"));
					entity.set("hkcity", TransApi.transliterate(entity.getStr("city"),"cht"));
					entity.set("cityjp", TransApi.transliterate(entity.getStr("city"),"jp"));
					entity.set("cityko", TransApi.transliterate(entity.getStr("city"),"kor"));
				}
				
//				if(StringUtils.isNotBlank(entity.getStr("country"))) {
//					entity.set("ecountry", TransApi.transliterate(entity.getStr("country"),"en"));
//					entity.set("hkcountry", TransApi.transliterate(entity.getStr("country"),"cht"));
//					entity.set("countryjp", TransApi.transliterate(entity.getStr("country"),"jp"));
//					entity.set("countryko", TransApi.transliterate(entity.getStr("country"),"kor"));
//				}
				
//				if(StringUtils.isNotBlank(entity.getStr("continent"))) {
//					entity.set("econtinent", TransApi.transliterate(entity.getStr("continent"),"en"));
//					entity.set("hkcontinent", TransApi.transliterate(entity.getStr("continent"),"cht"));
//					entity.set("continentjp", TransApi.transliterate(entity.getStr("continent"),"jp"));
//					entity.set("continentko", TransApi.transliterate(entity.getStr("continent"),"kor"));
//				}
				
				Entity where = new Entity();
				where.set("id", entity.get("id"));
				try {
					runner.update(conn, entity, where);
				} catch (Exception e) {
					e.printStackTrace();
					conn = ds.getConnection();
					errorIds.add(entity.get("id"));
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(StringUtils.join(errorIds, ","));
	}
	
	public static void updatePlatformAirports1() {
		String url = "jdbc:mysql://192.168.100.10:3308/trip_config?useUnicode=true&characterEncoding=UTF-8";
		String user = "root";
		String pass = "tarotrip@2024";
		SimpleDataSource ds = new SimpleDataSource(url, user, pass);
		SqlConnRunner runner = DbUtil.newSqlConnRunner(ds);
		
		List<Object> errorIds = new ArrayList<Object>();
		try {
		
			Connection conn = ds.getConnection();
			Entity query = new Entity("platform_airports");
			query.set("id1", new Condition("id", ">=", 1));
			
			List<Entity> entities = runner.find(ds.getConnection(), query);
			for (Entity entity : entities) {
				
				boolean needUpdate = false;
				if(!containsChinese(entity.getStr("name"))) {
					entity.set("name", TransApi.transliterate(entity.getStr("name"),"en","zh"));
					entity.set("hkname", TransApi.transliterate(entity.getStr("name"),"cht"));
					entity.set("namejp", TransApi.transliterate(entity.getStr("name"),"jp"));
					entity.set("nameko", TransApi.transliterate(entity.getStr("name"),"kor"));
					needUpdate = true;
				}
				
				if(!containsChinese(entity.getStr("city"))) {
					entity.set("city", TransApi.transliterate(entity.getStr("city"),"en","zh"));
					entity.set("hkcity", TransApi.transliterate(entity.getStr("city"),"cht"));
					entity.set("cityjp", TransApi.transliterate(entity.getStr("city"),"jp"));
					entity.set("cityko", TransApi.transliterate(entity.getStr("city"),"kor"));
					needUpdate = true;
				}
				if(needUpdate) {
					System.out.println(JSONUtil.toJsonStr(entity));
					Entity where = new Entity();
					where.set("id", entity.get("id"));
					try {
						runner.update(conn, entity, where);
					} catch (Exception e) {
						e.printStackTrace();
						conn = ds.getConnection();
						errorIds.add(entity.get("id"));
					}
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(StringUtils.join(errorIds, ","));
	}
	
	public static boolean containsChinese(String str) {
	    String regex = "[\\u4e00-\\u9fa5]";
	    return str.matches(".*" + regex + ".*");
	}
}
